package com.atguigu.stack;

/**
 * @author shkstart
 * @create 2021-09-20 9:46
 */
//package linklist;
import java.util.Scanner;
import java.util.Stack;

/**
 * 此题还可以引申至配对字符符匹配问题，如单引号，双引号匹配问题。
 *
 */

public class ExpStackMatching {

    public boolean matching(String expression)
    {
        if(expression==null||expression=="")
        {
            System.out.println( "输入表达式为空或没有输入表达式" ) ;
        }

        Stack<Character> stack = new Stack<Character>() ;

        for(int index=0 ; index<expression.length();index++)
        {
            switch(expression.charAt(index))
            {
                case '(':
                    stack.push(expression.charAt(index)) ;
                    break ;
                case '{':
                    stack.push(expression.charAt(index)) ;
                    break ;
                case ')':
                    if(!stack.empty()&&stack.peek()=='(')
                    {
                        stack.pop() ;
                    }
                    break ;

                case '}':
                    if(!stack.empty()&&stack.peek()=='{')
                    {
                        stack.pop();
                    }
            }
        }

        if(stack.empty())
            return true ;
        return false ;
    }

    public static void main(String[] args) {

//        String expression = "{((1+3)+2+4)+9*7}" ;
        System.out.println("请输入你要判断的式子：");
        Scanner scanner = new Scanner(System.in);
        String expression = scanner.next();
        ExpStackMatching oj = new ExpStackMatching() ;

        boolean flag = oj.matching(expression) ;

        if(flag)
        {
            System.out.println("匹配成功！") ;
        }
        else
        {
            System.out.println(" 匹配失败 ");
        }
    }



}
